//
//  ZDBTestViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/17.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBTastViewController.h"
#import "ZDBMyTastCell.h"
#import "ZDBTastModel.h"
#import "ZDBSubimtTaskViewController.h"
@interface ZDBTastViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic,strong) NoNetworkView * workView;
@end

@implementation ZDBTastViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    
    WeakSelf
       // 上拉加载
       MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           self.page =1;
           [weakSelf tastList];
       }];
       [header beginRefreshing];
       self.tableView.mj_header = header;
       
       MJRefreshBackStateFooter * foot = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            self.page ++;
            [weakSelf tastList];
       }];
       self.tableView.mj_footer = foot;
}
- (void)placeholderViewWithFrame:(CGRect)frame NoNetwork:(BOOL)NoNetwork
{
    if (self.dataSource.count == 0)
    {
        [_workView removeFromSuperview];
        _workView = [[NoNetworkView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) NoNetwork:NoNetwork];
        [self.tableView addSubview:_workView];
        //_workView.centerY = self.collectionView.centerY-100;
    }else
    {
        [_workView dissmiss];
    }
    
}
#pragma 获取任务列表
- (void)tastList
{
    WeakSelf
    NSString  * getUserReceiveList = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getUserReceiveList"];
    [[SmileHttpTool sharedInstance] POST:getUserReceiveList parameters:@{@"type":@"",@"pageNum":@(self.page),@"pageSize":@(10)} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
       [self.tableView.mj_header endRefreshing];
       [self.tableView.mj_footer endRefreshing];
        NSDictionary * dic = responseObject;
       
        if (!kDictIsEmpty(dic)) {
            NSInteger  pages = [dic[@"pages"] integerValue];//一共多少条数据
            NSArray * list = dic[@"list"];//
            if (weakSelf.page>pages)
            {
//                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }else
            {
                if (weakSelf.page > 1)
                {
                    [weakSelf.dataSource addObjectsFromArray:[ZDBTastModel mj_objectArrayWithKeyValuesArray:list]];
                    
                }else
                {
                    self.dataSource = [ZDBTastModel mj_objectArrayWithKeyValuesArray:list];
                }
               
            }
             [self.tableView reloadData];
            
        }else
        {

            [JMNotifyView showNotify:responseObject[@"message"] isSuccessful:NO];
        }
       [self placeholderViewWithFrame:self.tableView.frame NoNetwork:NO];
      
    } failure:^(id responseObject, NSError *error) {
       [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
  
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
#pragma  mark -----懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - kStatusBarAndNavigationBarHeight)style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = CELLCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource=self;
        _tableView.backgroundColor =  VIEWBGCOLOR;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = 0.001;
         _tableView.tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 0.0001)];
        [_tableView registerNib:[UINib nibWithNibName:@"ZDBMyTastCell" bundle:nil]  forCellReuseIdentifier:@"ZDBMyTastCell"];
    
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     ZDBTastModel * model = self.dataSource[indexPath.row];
    if (model.state.integerValue ==5) {
        return 130;
    }
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBMyTastCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ZDBMyTastCell" forIndexPath:indexPath];
    cell.model = _dataSource[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBTastModel * model = self.dataSource[indexPath.row];//任务状态 1=进行中 2=审核中 3=已完成 4=已过期 5=审核失败
    if (model.state.integerValue ==1 || model.state.integerValue ==5) {
        ZDBSubimtTaskViewController *subimtTaskVC = [[ZDBSubimtTaskViewController alloc] init];
        subimtTaskVC.title = @"提交任务";
        subimtTaskVC.model = model;
        subimtTaskVC.id = model.tiid;
        [self.navigationController pushViewController:subimtTaskVC animated:YES];
        WeakSelf
        subimtTaskVC.updetaTaskBlock = ^(BOOL isUpdate) {
            [weakSelf tastList];
        };
    }else if (model.state.integerValue ==2)
    {
        [JMNotifyView showNotify:@"任务正在审核中" isSuccessful:NO];
    }else if (model.state.integerValue ==4)
    {
         [JMNotifyView showNotify:@"任务已过期" isSuccessful:NO];
    }
    
}

@end
